package com.temple.controller;



import com.blog.security.exception.ServiceException;
import com.comon.core.domain.result.R;

import com.comon.core.enums.ResultCode;
import com.temple.mapper.circleMapper;
import com.temple.model.card.VO.SelfNumberInfo;
import com.temple.model.circle.CircleInfo;
import com.temple.model.circle.DTO.MyCircleDTO;
import com.temple.model.circle.DTO.PopularTemple;
import com.temple.model.favor.UserFavor;
import com.temple.service.CircleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;

import com.temple.model.circle.DTO.CircleDTO;
/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-09-06
 * Time:15:52
 * Text：
 */
@RestController
@Tag(name="殿堂注册API")
public class CircleController {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private CircleService circleService;


    @Operation(summary = "创建殿堂")
    @ApiResponse(responseCode = "4000",description = "图片为空")
    @ApiResponse(responseCode = "4001",description = "殿堂已存在")
    @ApiResponse(responseCode = "4002",description = "数据保存失败")
    @PostMapping("/circleImg")
    public R<?> circleImg(@RequestPart("circleDTO") String circleDTO,
                          @RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        // 判断是否为空文件
        if (file.isEmpty()) {
            return R.fail(4000,"未上传图片");
        }
        Long Uid = Long.parseLong(request.getSession().getAttribute("UserId").toString());
        System.out.println(circleDTO);
        //获得circleDTO
        CircleDTO circleDTO1 = objectMapper.readValue(circleDTO, CircleDTO.class);
        return circleService.createTemple(circleDTO1,file,Uid);
    }

    @PostMapping("/EditTemple")
    public R<?> EditTemple(@RequestPart("circleDTO") String circleDTO,
                          @RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        // 判断是否为空文件
        if (file.isEmpty()) {
            return R.fail(4000,"未上传图片");
        }
        Long Uid = Long.parseLong(request.getSession().getAttribute("UserId").toString());
        System.out.println(circleDTO);

        // 将 circleDTO 中的 uid 替换为 uId
        String updatedCircleDTO = circleDTO.replace("\"uid\":", "\"uId\":");

        //获得circleDTO
        CircleInfo circleDTO1 = objectMapper.readValue(updatedCircleDTO, CircleInfo.class);
        return circleService.EditTemple(circleDTO1,file,Uid);
    }


    @GetMapping("/getMyTemple")
    @Operation(summary = "获得自己殿堂信息")
    public R<List<MyCircleDTO>> getMyTemple(HttpServletRequest request){
        return circleService.getMyTemple(Long.parseLong(request.getSession().getAttribute("UserId").toString()));
    }
    @GetMapping("/getTempleNumberInfo/{id}")
    @Operation(summary = "获得自己殿堂的人员信息")
    public R<?> getMyTemple(@PathVariable int id){
        return circleService.getTempleNumberInfo(id);
    }



    @GetMapping("/editTemple")
    public R<?> editTemple(){
        return null;
    }

    @GetMapping("/getTemple")
    @Operation(summary = "获得热门殿堂信息")
    public R<List<PopularTemple>> getTemple(){
        return circleService.getTemple();
    }

    @GetMapping("/getTempleInfo/{id}")
    @Operation(summary = "获取殿堂信息",description = "根据殿堂ID获取数据")
    public R<?> getTempleInfo(@PathVariable Long id,HttpServletRequest request){
        try {
            Long u_id = Long.parseLong(request.getSession().getAttribute("UserId").toString());
            return circleService.getTempleInfo(id,u_id);
        }catch (Exception e){
            return R.fail(ResultCode.FAILED_FORBIDDEN);
        }
    }
    @GetMapping("/getEditTemple/{id}")
    @Operation(summary = "获取编辑殿堂信息",description = "根据殿堂ID获取数据")
    public R<?> getEditTemple(@PathVariable Long id){
            return circleService.getEditTemple(id);
    }

    @GetMapping("/setAttention/{id}")
    public R<?> setAttention(@PathVariable int id,HttpServletRequest request){
        Long u_id = Long.parseLong(request.getSession().getAttribute("UserId").toString());
        return circleService.setAttention(id,u_id);
    }
    @GetMapping("/unAttention/{id}")
    public R<?> unAttention(@PathVariable int id,HttpServletRequest request){
        Long u_id = Long.parseLong(request.getSession().getAttribute("UserId").toString());
        return circleService.unAttention(id,u_id);
    }

}
